package com.bjdd.jdy.ui.Mine;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseFragment;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.entity.result.PerformanceResult;
import com.bjdd.jdy.util.ImageUtil;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.math.RoundingMode;
import java.text.NumberFormat;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.toolbar)
    AutoRelativeLayout toolbar;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_mobile)
    TextView tvMobile;
    @Bind(R.id.tv_assess)
    TextView tvAssess;
    @Bind(R.id.all_assess)
    AutoLinearLayout allAssess;
    @Bind(R.id.tv_order_num)
    TextView tvOrderNum;
    @Bind(R.id.all_order_num)
    AutoLinearLayout allOrderNum;
    @Bind(R.id.all_mine_order)
    AutoLinearLayout allMineOrder;
    @Bind(R.id.all_mine_wallet)
    /*AutoLinearLayout allMineWallet;
    @Bind(R.id.all_mine_assess)*/
    AutoLinearLayout allMineAssess;
    @Bind(R.id.all_mine_emergency)
    AutoLinearLayout allMineEmergency;
    @Bind(R.id.all_mine_about_us)
    AutoLinearLayout allMineAboutUs;
    @Bind(R.id.all_mine_setting)
    AutoLinearLayout allMineSetting;
    private User user;
    private ImageUtil imageUtil;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(getActivity(), Color.parseColor("#FFDF22"),0);
        toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
        title.setText("我的");
        toolbarBack.setVisibility(View.GONE);
        imageUtil = new ImageUtil(context);
        userInfoBack();
        presenter.getUserInfo();
    }


    @OnClick({R.id.img_head, R.id.all_assess, R.id.all_order_num, R.id.all_mine_order, R.id.all_mine_wallet, R.id.all_mine_emergency, R.id.all_mine_about_us, R.id.all_mine_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_head:
                break;
            case R.id.all_assess:
                break;
            case R.id.all_order_num:
                break;
            case R.id.all_mine_order:
                JumpUtil.toOrder(context);
                break;
            case R.id.all_mine_wallet:
                JumpUtil.toWallet(context);
                break;
            case R.id.all_mine_emergency:
                JumpUtil.toEmergency(context);
                break;
            case R.id.all_mine_about_us:
                JumpUtil.toAboutUs(context);
                break;
            case R.id.all_mine_setting:
                JumpUtil.toSetting(context);
                break;
        }
    }

    @Override
    public void userInfoBack() {
        user = SpUtil.getUser();
        if (user!=null || user.getCarrierId()!=null){
            tvMobile.setText(user.getMobile());
            tvName.setText(user.getRealName());
            if (user.getPhoto()!=null){
                imageUtil.loadCircleImage(user.getPhoto(),imgHead);
            }
        }
    }

    @Override
    public void performanceSuccess(PerformanceResult result) {
        if (result == null)
            return;
        tvOrderNum.setText(result.getOrderCount()+"");
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.UP);
        tvAssess.setText(nf.format(result.getStarCount())+"");
    }

}
